﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Linq;
using System.Security.Claims;


namespace MDT.LifeSewagePlatform.Conmmon
{
    public class CustomActionFilter : ActionFilterAttribute
    {
        //默认管理员权限
        public RoleType Role { get; set; }
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            
            var controllerName = context.ActionDescriptor.RouteValues["controller"];
            var role = context.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role);

            if (role == null)
            {
                context.Result = new ObjectResult(new { code = 401, errormsg = "未经授权,请联系管理员!" });
                return;
            }
            int.TryParse(role.Value, out int roleValue);

            //无权限访问
            if ((int)Role < roleValue)
            {
                context.Result = new ObjectResult(new { code = 401, errormsg = "未经授权,请联系管理员!" });
            }

        }

        public override void OnResultExecuted(ResultExecutedContext context)
        {
        }

    }
}
